package com.example.day4_fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

/***
 事物：transaction
 1。add（）：添加
 2。show（）：显示
 3。hide（）：隐藏

 */
public class MusicActivity extends AppCompatActivity {
    RadioGroup radioGroup;
    BlankFragment blankFragment;
    BlankFragment2 blankFragment2;
    BlankFragment3 blankFragment3;
    BlankFragment4 blankFragment4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        radioGroup = findViewById(R.id.group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                hideAll();
                switch (checkedId){
                    case R.id.rb1://显示第一个fragment
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        if(blankFragment != null){//判断是否加过
                            transaction.show(blankFragment);//直接显示
                        }else{//没有加过
                            blankFragment = new BlankFragment();
                            transaction.add(R.id.frame,blankFragment);
                        }
                        transaction.commit();//千万不要忘记提交
                        break;
                    case R.id.rb2:

                        FragmentManager manager2 = getSupportFragmentManager();
                        FragmentTransaction transaction2 = manager2.beginTransaction();
                        if(blankFragment2 != null){//判断是否加过
                            transaction2.show(blankFragment2);//直接显示
                        }else{//没有加过
                            blankFragment2 = new BlankFragment2();
                            transaction2.add(R.id.frame,blankFragment2);
                        }
                        transaction2.commit();//千万不要忘记提交
                        break;
                    case R.id.rb3:
                        FragmentManager manager3 = getSupportFragmentManager();
                        FragmentTransaction transaction3 = manager3.beginTransaction();
                        if(blankFragment3!= null){//判断是否加过
                            transaction3.show(blankFragment3);//直接显示
                        }else{//没有加过
                            blankFragment3 = new BlankFragment3();
                            transaction3.add(R.id.frame,blankFragment3);
                        }
                        transaction3.commit();//千万不要忘记提交

                        break;
                    case R.id.rb4:
                        FragmentManager manager4 = getSupportFragmentManager();
                        FragmentTransaction transaction4 = manager4.beginTransaction();
                        if(blankFragment4!= null){//判断是否加过
                            transaction4.show(blankFragment4);//直接显示
                        }else{//没有加过
                            blankFragment4 = new BlankFragment4();
                            transaction4.add(R.id.frame,blankFragment4);
                        }
                        transaction4.commit();//千万不要忘记提交
                        break;
                }
            }
        });
    }
    public void hideAll(){
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        if(blankFragment != null){
            transaction.hide(blankFragment);
        }
        if(blankFragment2 != null){
            transaction.hide(blankFragment2);
        }
        if(blankFragment3 != null){
            transaction.hide(blankFragment3);
        }
        if(blankFragment4 != null){
            transaction.hide(blankFragment4);
        }
        transaction.commit();
    }
}
